using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace MTRC.ODMS.Web.SOM.Popup
{
    /// <summary>
    /// author:Nearly
    /// </summary>
    public partial class PopupSearchStaff : BasePage_SOM
    {
        #region  Page Load and UnLoad
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                // add by dylan 20151030 for u275.1 
                this.grdPopSearchStaff.PageSize = this.GridViewPageDefaultSize();
                /*QueryString["EventPersonTypeID"]
                =1  Get Staff
                =2  Get CQA Contractor
                =3  Get Station Contractor
                =NULL Get Staff/CQA Contractor/Station Contractor
               */

                if (string.Concat(Request.QueryString["EventPersonTypeID"]) != string.Empty)
                {
                    int intEventPersonTypeID = Convert.ToInt32(string.Concat(Request.QueryString["EventPersonTypeID"]));
                    BindListControl(this.rblStaffType, "EventPersonTypeID", "Description", ODMS.DAL.SOM.CodeSetup.CDEventPersonType.GetEventPersonTypeInSearchWindow(this.LangCode, intEventPersonTypeID), false);
                }
                else
                {
                    //huangyao 20090209: for new change
                    // In staff searching dialog window (that popup from 'Admission Registry Event', 'Fire protection event', 'Asset Loan event', 'General Event'):
                    //-- only allow search by 'Station contractor'
                    //------------so hard code EventPersonTypeID=3
                    //BindListControl(this.rblStaffType, "EventPersonTypeID", "Description", ODMS.DAL.SOM.CodeSetup.CDEventPersonType.GetEventPersonTypeInSearchWindow(this.LangCode, 0), false);
                    BindListControl(this.rblStaffType, "EventPersonTypeID", "Description", ODMS.DAL.SOM.CodeSetup.CDEventPersonType.GetEventPersonTypeInSearchWindow(this.LangCode, 3), false);
                }

                if (rblStaffType.Items.Count > 0)
                {
                    this.rblStaffType.Items[0].Selected = true;
                }
                else
                {
                    this.btnSearch.Disabled = true;
                }

                // add by dylan 20150827 for u168.1
                //udpate by gordon 20151020 U247
                //BindListControl(this.ddlDepartmentCompany, "DepartCompany", "DepartCompany", MTRC.ODMS.DAL.SOM.CodeSetup.CdStaffInfo.GetDepartmentCompany(this.LangCode, this.ODMUser.StationID,Convert.ToInt32(this.rblStaffType.SelectedValue)), true, "");
                BindListControl(this.ddlDepartmentCompany, "DepartCompany", "DepartCompany", MTRC.ODMS.DAL.SOM.CodeSetup.CdStaffInfo.GetDepartmentCompany(this.LangCode, this.ODMUser.LocationID, Convert.ToInt32(this.rblStaffType.SelectedValue)), true, "");

                SetLanguage();
            }
        }

        private void SetLanguage()
        {
            this.lblStaffType.Text = this.GetLabel("SOM_SearchStaffType", true); //Modify by zengshunbo issue 1049
            //this.lblStaffID.Text = this.GetLabel("SOM_SearchStaffID", true); //Modify by zengshunbo issue 1049
            this.lblStaffName.Text = this.GetLabel("SOM_SearchStaffName", true); //Modify by zengshunbo issue 1049
            //this.lblContactNo.Text = this.GetLabel("COM_ContactNo", true);
            //this.lblEmail.Text = this.GetLabel("COM_Email", true);
            //this.lblDepartmentCompany.Text = this.GetLabel("SOM_DepartmentCompany", true); //huangyao 20090209: new change
            this.lblDepartmentCompany.Text = this.GetLabel("SOM_ContractorCompany", true);  //huangyao 20090209: new change./ update by benson 2015-08-26 SOM_ContractorCompany
            this.btnSearch.Value = this.GetLabel("COM_Search");
            this.btnColse.Value = this.GetLabel("COM_Close");

        }

        private void ChangeLanguage_Click(object sender, System.EventArgs e)
        {
            SetLanguage();
        }

        #endregion

        #region Data Grid Bind

        private void btnSearch_ServerClick(object sender, EventArgs e)
        {
            BindData();
        }

        private void BindData()
        {
            //BasePage bp=new BasePage();
            int intCurrentUsergroup = this.ODMUser.UserGroupID;
            int intStaffType = Convert.ToInt32(this.rblStaffType.SelectedValue);
            //string strStaffID = "";// this.txtStaffID.Text;
            string strStaffName = this.txtStaffName.Text;
            // edit by dylan 20150827 for u168.1
            //string strDepartmentCompany = this.txtDepartmentCompany.Text;
            string strDepartmentCompany = this.ddlDepartmentCompany.SelectedValue;
            //string strTel = "";// this.txtContactNo.Text;
            //string strEmailAddress = "";// this.txtEmail.Text;

            DataTable dataTable;
            //update by gordon 20151020 U247
            //dataTable = MTRC.ODMS.DAL.SOM.CodeSetup.CdStaffInfo.GetSearchStaff(this.LangCode, this.ODMUser.StationID, intStaffType, strStaffName, strDepartmentCompany);
            dataTable = MTRC.ODMS.DAL.SOM.CodeSetup.CdStaffInfo.GetSearchStaff(this.LangCode, this.ODMUser.LocationID, intStaffType, strStaffName, strDepartmentCompany);
            DataView dataView = dataTable.DefaultView;
            this.grdPopSearchStaff.GetLabelFunction = new APJ.Web.UI.WebControls.GetLabel(this.GetLabel);
            this.grdPopSearchStaff.DataSource = dataTable.DefaultView;
            this.grdPopSearchStaff.DataBind();

            dataView = null;
            dataTable = null;
        }
        //add by benson 2015-10-23 u250 begon
        public static string ScriptStrHandler2(string str)
        {
            return str.Replace("\'", "\\\'");
        }
        //add by benson 2015-10-23 u250 end
        protected void grdPopSearchStaff_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.DataRow)
            {
                string strEventPersonType = ((DataBoundLiteralControl)e.Row.Cells[6].Controls[0]).Text.Replace("\r\n", "").Trim();
                string strStaffType = this.rblStaffType.SelectedValue.ToString();
                //add by SQB
                string strStaffTypeDesc = this.rblStaffType.SelectedItem.Text.Trim();
                //add by SQB
                e.Row.Attributes.Add("onmouseover", "this.oldcolor=this.style.backgroundColor;this.style.backgroundColor='#eaffd9';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.oldcolor");
                //e.Row.Attributes.Add("OnClick", "setStaffInfor('" + DataBinder.Eval(e.Row.DataItem, "StaffID").ToString() + "','" + DataBinder.Eval(e.Row.DataItem, "StaffName").ToString() + "', '" + DataBinder.Eval(e.Row.DataItem, "DepartmentCompany").ToString() + "', '" + DataBinder.Eval(e.Row.DataItem, "OrganizationUnit").ToString() + "', '" + DataBinder.Eval(e.Row.DataItem, "Tel").ToString() + "', '" + strStaffType + "', '" + strEventPersonType + "')");
                string staffOrExtPartyID = ((System.Data.DataRowView)e.Row.DataItem)["StaffOrExtPartyID"].ToString();
                e.Row.Attributes.Add("OnClick", "setStaffInfor('" + ScriptStrHandler2(DataBinder.Eval(e.Row.DataItem, "StaffID").ToString())
                        + "','" + ScriptStrHandler2(DataBinder.Eval(e.Row.DataItem, "StaffName").ToString())
                        + "', '" + ScriptStrHandler2(DataBinder.Eval(e.Row.DataItem, "DepartmentCompany").ToString())
                        + "', '" + ScriptStrHandler2(DataBinder.Eval(e.Row.DataItem, "OrganizationUnit").ToString())
                        + "', '" + ScriptStrHandler2(DataBinder.Eval(e.Row.DataItem, "Tel").ToString())
                    //+ "', '" + DataBinder.Eval(e.Row.DataItem, "StaffOrExtPartyID").ToString() 
                        + "', '" + ScriptStrHandler2(strStaffType)
                        + "', '" + ScriptStrHandler2(staffOrExtPartyID)
                        + "', '" + ScriptStrHandler2(strEventPersonType)
                        + "', '" + ScriptStrHandler2(strStaffTypeDesc)
                        + "', '" + ScriptStrHandler2(DataBinder.Eval(e.Row.DataItem, "ExtPartyID").ToString())
                        + "', '" + ScriptStrHandler2(((System.Data.DataRowView)e.Row.DataItem)["ExtPartyStaffSnapShotID"].ToString()) //add by hzx,20090104
                        + "');");

                if (strStaffType == "2")
                {
                    strStaffType = this.grdPopSearchStaff.DataKeys[e.Row.RowIndex][0].ToString(); ;
                }
                else
                {
                    strStaffType = e.Row.Cells[0].Text;
                }
                System.Web.UI.WebControls.HyperLink hl_StaffID = new System.Web.UI.WebControls.HyperLink();
                hl_StaffID.Text = strStaffType;
                e.Row.Cells[0].Controls.Clear();
                e.Row.Cells[0].Controls.Add(hl_StaffID);
            }
        }

        private void grdPopSearchStaff_NeedDataBind()
        {
            BindData();
        }

        #endregion

        #region Web Form Designer generated code

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            this.ChangeLanguage += new MTRC.ODMS.Web.BasePage.ChangeLanguageEventHandler(this.ChangeLanguage_Click);
            this.btnSearch.ServerClick += new EventHandler(btnSearch_ServerClick);
            this.grdPopSearchStaff.NeedDataBind += new APJ.Web.UI.WebControls.CommonGridView.NeedDataBindHandler(grdPopSearchStaff_NeedDataBind);
        }

        #endregion
    }
}
